import Message from "element-ui/packages/message/src/main";
import Notification from "element-ui/packages/notification/src/main";
import * as nodeAPI from "../../api/nodeAPI";
import global from "../global";

const nodeStorage = {
    state: {
        new_node_flag: false,
    },
    mutations: {
        set_new_node_flag: function (state, flag) {
            if (flag === state.new_node_flag) {
                return;
            }
            if (flag) {
                let notice = Notification({
                    ...global.noticeConfig,
                    message: "点击图谱任意位置生成节点"
                });
                global.noticeList.push(notice);
            } else {
                global.noticeList.pop().close();
            }
            state.new_node_flag = flag;

        },

    },
    actions: {
        getGraphNodesById: async ({commit}, graphId) => {
            let res = await nodeAPI.getGraphNodesByIdAPI(graphId);
            if (!res) {
                console.log("图节点获取失败");
            }
            return res;
        },
        getNodeById: async ({commit}, nodeId) => {
            let res = await nodeAPI.getNodeByIdAPI(nodeId);
            if (!res) {
                console.log("节点获取失败");
            }
            return res;
        },
        searchGraphNodes: async ({commit}, params) => {
            let res = await nodeAPI.searchGraphNodesAPI({graphId: params.graphId, name: params.name});
            return res;
        },
        addNode: async ({commit}, node) => {
            let res = await nodeAPI.addNodeAPI(node);
            if (!res) {
                Message({
                    message: "节点添加失败",
                    type: "error"
                });
            }
            return res;
        },
        /*params.name:graphName*/
        addNodeByName: async ({commit}, params) => {
            let res = await nodeAPI.addNodeByNameAPI({graphId: params.graphId, name: params.name});
            if (!res) {
                Message({
                    message: "节点添加失败",
                    type: "error"
                });
            }
            return res;
        },
        deleteNodeById: async ({commit}, graphId) => {

            let res = await nodeAPI.deleteNodeByIdAPI(graphId);
            if (!res) {
                Message({
                    message: "节点删除失败",
                    type: "error"
                });
            }
        },
        updateNode: async ({commit}, node) => {

            let res = await nodeAPI.updateNodeAPI(node);
            if (!res) {
                Message({
                    message: "节点修改失败",
                    type: "error"
                });
            }
            return res;
        }
    }
};

export default nodeStorage;